/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include "i2c.h"
#include "console.h"
#include "debug.h"
#include "syslog.h"
#include "kassert.h"


static int i2c_ioctl(FD fd, int cmd, void* args)
{
    int ret = OK;
    struct I2cData *data;
    struct I2c *bus = (I2c*)fd;

    switch (cmd)
    {
    /* set 10-bit addr mode */
    case I2C_DEV_CTRL_10BIT:
        bus->Protected.flags |= I2C_ADDR_10BIT;
        break;
    case I2C_DEV_CTRL_ADDR:
        bus->Protected.addr = *(uint16_t *)args;
        break;
    case I2C_DEV_CTRL_TIMEOUT:
        bus->Protected.timeout = *(uint32_t *)args;
        break;
    case I2C_DEV_CTRL_RW:
        data = (struct I2cData *)args;
        ret = I2cTransfer(bus, data->msgs, data->number);
        break;
    default:
        break;
    }

    return ret;
}

void I2cCreate(I2c* i2c, const char *name)
{
    i2c->super.popen = NULL;
    i2c->super.pioctl = i2c_ioctl;
    i2c->super.pread = NULL;
    i2c->super.pwrite = NULL;
    i2c->super.pclose = NULL;

    atomMutexCreate(&i2c->Protected.lock);
    IosDrvInstall(&i2c->super, name);
}

size_t I2cMasterSend(I2c* bus,  uint16_t addr, uint16_t flags, const uint8_t* buf, uint32_t count)
{
    size_t ret;
    struct i2c_msg msg;

    msg.addr = addr;
    msg.flags = flags;
    msg.len = count;
    msg.buf = (uint8_t*)buf;

    ret = I2cTransfer(bus, &msg, 1);

    return (ret > 0) ? count : ret;
}

size_t I2cMasterRecv(I2c* bus, uint16_t addr, uint16_t flags, uint8_t* buf, uint32_t count)
{
    size_t ret;
    struct i2c_msg msg;

    msg.addr = addr;
    msg.flags = flags | I2C_RD;
    msg.len = count;
    msg.buf = buf;

    ret = I2cTransfer(bus, &msg, 1);

    return (ret > 0) ? count : ret;
}

size_t I2cTransfer(I2c *bus, struct i2c_msg msgs[], uint32_t num)
{
    size_t ret;

    if (bus->Protected.ops->master_xfer)
    {
        atomMutexGet(&bus->Protected.lock, 0);
        ret = bus->Protected.ops->master_xfer(bus, msgs, num);
        atomMutexPut(&bus->Protected.lock);

        return ret;
    }
    else
    {
        SYSLOG(LOG_ERR, "I2C bus operation not supported");
        return 0;
    }
}